const appid='592fafa1923c4d00969bef7f434091db';
const appkey='jkQfa97Wvayhid6AR1f8wBBQJGx2RD';



let token = generateToken();

function generateToken() {
  const currentTime = Math.floor(Date.now() / 10000).toString();
  const keyString = `${appid}${appkey}${currentTime}`;
  return CryptoJS.MD5(keyString).toString(CryptoJS.enc.Hex);
}

const httpService = axios.create({
  baseURL: 'http://218.31.35.46:9090/bigscreen',
  timeout: 10000,
});

httpService.interceptors.request.use(config => {
  config.headers['Authorization'] = 'Xbzh-' + token;
  config.headers['Appid'] = appid;
  return config;
}, error => {
  return Promise.reject(error);
});

httpService.interceptors.response.use(response => {
  return response.data;
}, async error => {
  const originalRequest = error.config;

  // 处理401错误，尝试刷新令牌
  if (error.response && error.response.status === 401) {
    try {
      // 刷新令牌的请求
      token = await refreshToken(); // 实现刷新令牌的逻辑
      originalRequest.headers['Authorization'] = 'Xbzh-' + token;
      return httpService(originalRequest); // 重新发送请求
    } catch (refreshError) {
      return Promise.reject(refreshError);
    }
  }

  return Promise.reject(error);
});

async function refreshToken() {

  return generateToken();
}
export default httpService
export function get(url, params) {
  return httpService.get(url, { params });
}

export function post(url, data) {
  return httpService.post(url, data);
}